<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2018/6/21
 * Time: 10:37
 */

namespace app\index\controller;

use app\index\Controller;
use think\Db;
use think\Validate;

/**
 * @title 文章（模块20）
 * @description 文章
 * @group 文章
 * @header name:model_id   default:05 desc:模块id
 * @groupremark 已使用错误码：1200001 1200002
 */
class CarArticle extends Controller
{
    /**
     * @title 获取文章列表
     * @description 接口说明
     * @author 宋晓文
     * @url /index/car_article/get_list
     * @param name:token type:string require:1  other: desc:token
     * @method ~
     * @return data: 文章列表（type：0-文章；1-链接）
     * @return error_id: 1200002 查询无数据
     */
    public function get_list(){
        $res = Db::name('CarArticle')->where('status','=',1)->field(['id','title','content','type'])->order('sort','asc')->select();

        if($res){
            $msg['list'] = $res;
            $msg['message'] = '查询成功';
            return $this->success($msg);
        }else{
            $msg['error_id']="1200002";
            $msg['message'] = 'get_list:no return';
            return $this->error($msg);
        }
    }

    /**
     * @title 获取文章详情
     * @description 接口说明
     * @author 宋晓文
     * @url /index/car_article/info
     * @param name:token type:string require:1  other: desc:token
     * @param name:article_id type:int require:1  other: desc:文章id
     * @method ~
     * @return data: 文章内容
     * @return error_id: 1200001 验证不通过
     * @return error_id: 1200002 查询无数据
     */
    public function info(){

        $input = input();

        $validate = new Validate([
            ['token','require','缺少token'],
            ['article_id','require','未指定文章'],
        ]);

        if(!$validate->check($input)){
            $msg['error_id']="1200001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $result = Db::name('CarArticle')
            ->where('id','=',$input['article_id'])
            ->where('status','=',1)
            ->where('type','=',0)
            ->find();


        if($result){
            $msg['data'] = $result;
            $msg['message'] = '查询成功';
            return $this->success($msg);
        }else{
            $msg['error_id'] = '1200002';
            $msg['message'] = '文章不存在';
            return $this->error($msg);
        }
    }





}